﻿using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace Wild2.Models
{
    public class Logger
    {
        public static void Log(String page, String text, String trace, Int32? userId)
        {
            if (page.Length > 200) page = page.Substring(0, 500);
            text = HttpContext.Current.Request.UserHostAddress + " " + text;
            if (text.Length > 500) text = text.Substring(0, 500);
            if (trace.Length > 4000) trace = trace.Substring(0, 4000);
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectStatistic"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO [Errors]([Page], [Text], [Trace], [Date], [UserID]) VALUES(@Page, @Text, @Trace, GETDATE(), @UID)", conn))
                {
                    if (!String.IsNullOrEmpty(page)) cmd.Parameters.Add("@Page", System.Data.SqlDbType.NVarChar, 200).Value = page;
                    else cmd.Parameters.Add("@Page", System.Data.SqlDbType.NVarChar, 200).Value = DBNull.Value;
                    cmd.Parameters.Add("@Text", System.Data.SqlDbType.NVarChar, 500).Value = text;
                    cmd.Parameters.Add("@Trace", System.Data.SqlDbType.NVarChar, 4000).Value = trace;
                    if(userId.HasValue) cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = userId.Value;
                    else cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = DBNull.Value;
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
            }
        }

        public static void Log(Exception e, Int32? userId)
        {
            String text = e.Message;
            String page = null;
            try { page = HttpContext.Current.Request.RawUrl; }
            // ReSharper disable EmptyGeneralCatchClause
            catch (Exception) { }
            // ReSharper restore EmptyGeneralCatchClause
            String trace = e.StackTrace;
            if (page != null && page.Length > 200) page = page.Substring(0, 500);
            text = HttpContext.Current.Request.UserHostAddress + " " + text;
            if (text.Length > 500) text = text.Substring(0, 500);
            if (trace.Length > 4000) trace = trace.Substring(0, 4000);
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectStatistic"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO [Errors]([Page], [Text], [Trace], [Date], [UserID]) VALUES(@Page, @Text, @Trace, GETDATE(), @UID)", conn))
                {
                    if (!String.IsNullOrEmpty(page)) cmd.Parameters.Add("@Page", System.Data.SqlDbType.NVarChar, 200).Value = page;
                    else cmd.Parameters.Add("@Page", System.Data.SqlDbType.NVarChar, 200).Value = DBNull.Value;
                    cmd.Parameters.Add("@Text", System.Data.SqlDbType.NVarChar, 500).Value = text;
                    cmd.Parameters.Add("@Trace", System.Data.SqlDbType.NVarChar, 4000).Value = trace;
                    if (userId.HasValue) cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = userId.Value;
                    else cmd.Parameters.Add("@UID", System.Data.SqlDbType.Int).Value = DBNull.Value;
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                }
            }
        }
    }
}